package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.SysLogMapper;
import com.qf.agro.mapper.SysUserMapper;
import com.qf.agro.pojo.SysLog;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/7/3 11:49
 */
@Service
public class SysLogServiceImpl implements SysLogService {


    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public ResultInfo getPage(int page, int pageSize) {


        //去数据库查用户的公司
        SysUser sysUser = SysUserHolder.get();
        LambdaQueryWrapper<SysUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getId,sysUser.getId());
        List<SysUser> sysUsers =sysUserMapper.selectList(lqw);

        if(sysUsers==null){
            return ResultInfo.error("查询失败");
        }

        SysUser user = sysUsers.get(0);
        Long companyId = user.getCompanyId();
        LambdaQueryWrapper<SysLog> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(SysLog::getCompanyId,companyId);
        Page<SysLog> logPage=new Page<>(page,pageSize);
        Page<SysLog> sysLogPage = sysLogMapper.selectPage(logPage, lqw2);

        return ResultInfo.success(sysLogPage);
    }
}
